<script>
	export default {
		data() {
			return {
				content: '',
				messageId: '',
				testJson: {},
				testJson_null:{},
				msgType:'',
				whether:true,
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			const HC_names = uni.getStorageSync('HC_name');
			if(HC_names!=''){
				uni.switchTab({
					url: '/pages/index/index'
				});
			}else{
				uni.showToast({
					title: '请先登录',
					mask: true,
					icon: "none"
				});
				
			}
			// #ifdef APP-PLUS
			     // 锁定屏幕方向
			      plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		onShow: function() {
			// #ifdef APP-PLUS
			this.notificationToGo();
			// #endif
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		onError: function(err) {
			console.log('App异常', err)
		},
		globalData: {
			testJson: {
				messageId: '',
				content: '',
			},
			groupHouseNo:'',
			yaoshang:{},
			whether:true,
			// 监听到有新服务器消息
			getSocketMsg(reData) {
				// 监听到服务器消息
				// console.log('收到服务器消息', reData);
				const data = JSON.parse(reData)
				try {
					const data1 = JSON.parse(data.msg)
					// console.log(data.msgType)
					if (data.msgType=="sys.notify") {
						this.whether=true
						this.messageId = data1.messageId
						this.content = data1.content;
					}
					else if (data.msgType=="com.plugins.pending.open") {
						this.whether=true
						this.content =data1.title
					}
					else if (data.msgType=="pension.com.alarm.entity.Alarm") {
						let houseNo=data1.houseNo.split("-")
						this.groupHouseNo=uni.getStorageSync("groupHouseNo")
						let houeno=this.groupHouseNo.split(",")
						for (let i = 0; i < houeno.length; i++) {
							if(houeno[i]==houseNo[0]){
								this.whether=true
								if(data1.devName=="sos报警"){
									this.content =data1.memberName+data1.alarmDetail
								}else{
									this.content =data1.houseNo+data1.devName+data1.alarmDetail
								}
								if(data1.status=="1"){
									this.content=this.content+"已解除警告"
								}
							}
						}
					}
					//目前不需要推出信息
					// else if (data.msgType=="com.rabbitMQ.DirectMessListener.health.notice") {
					// 	this.whether=true
					// 	this.content=data1.name+"膳食提醒"
					// 	var term=data1.term
					// 	try {
					// 		//1. 申请资源和要执行的代码逻辑
					// 		this.yaoshang[term] = uni.getStorageSync('yaoshang' +term);
					// 	} catch (e) {
					// 		//2. 捕获到的异常
					// 		// e.printStackTrace();
					// 	}
					// 	console.log(this.yaoshang[term])
					// 	if(this.yaoshang[term]==null){
					// 		this.yaoshang[term]=[]
					// 	}
					// 	if(this.yaoshang[term].length==4){
					// 		this.yaoshang[term].shift();
					// 		this.yaoshang[term][3]=data1
					// 	}else{
					// 		// this.yaoshang.[this.i]=data1
					// 		this.yaoshang[term].push(data1)
					// 		// this.i++
					// 	}
					// 	console.log(data1)
					// 	//缓存药膳菜单
					// 	uni.setStorageSync('yaoshang'+term, this.yaoshang[term]);
					// }
					this.msgType=data.msgType
				} catch (e) {
					// console.log('收到错误消息', e);devId 6ceef3be1db638b164bgia
				}
				if(this.whether==true){
					if (data.code == "99") {
						if (data.msg != 'ping') {
							this.testJson.messageId = this.messageId
							this.testJson.content = this.content
							this.testJson.msgType = this.msgType
							this.whether=false
							if(this.testJson.content!=null){
								// console.log(this.testJson.content)
							// #ifdef APP-PLUS
							const module = uni.requireNativePlugin("WT-PushModule");
							const n = module.appPush({
								notificationArray: this.testJson
							});
							// console.log(n)
							// #endif
						}
					}
					}
				}
			},
		},
		methods: {
			notificationToGo() {
				if (plus.runtime.arguments != "" && plus.runtime.arguments != null) {
					const androidIntent = JSON.parse(plus.runtime.arguments);
					const NotificationUrl = Object.values(androidIntent);
					console.log(NotificationUrl)
					uni.navigateTo({
						url: NotificationUrl[0]
					});
					plus.runtime.arguments = null

					this.getUser();

				}
			},
			async getUser() {
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-portal/currentUser'
					})
				if (res.success == false) { //如果读取信息成功
					uni.removeTabBarBadge({
						index: 1,
					})
				} else if (res.success == true) {
					this.id = res.body.currentUser.id;
					this.checkopenSocket()
					this.news()
				}
			},
			async news() {
				const res = await this.$myRequest({
					url: '/api-msg/messages/',
					method: "get",
					data: {
						isReaded: 0,
					}
				})
				if (res.success == true) {
					this.total = res.data.totalSize
					var quantity
					if (this.total == 0) {
						uni.removeTabBarBadge({
							index: 1,
						})
					} else if (this.total >= 99) {
						quantity = "99+"
					} else {
						quantity = this.total
						quantity = quantity.toString()
					}
					uni.setTabBarBadge({
						index: 1,
						text: quantity,
					})
				}
			},
			//通知推送
			//判断是否已连接
			checkopenSocket() {
				uni.sendSocketMessage({
					data: "ping",
					success: res => {
						return
					},
					fail: err => {
						this.socket()
					}
				});
			},
			//打开连接
			socket() {
				uni.connectSocket({
					url: 'ws://pm.gxtimeloit.com:10034/websocket/' + this.id + '/all',
					// url: 'ws://pm.gxtimeloit.com:10034/websocket/2/all',
					// url: 'ws://icewould.tpddns.cn:18123',
					success: data => {
						// console.log(data)
					}
				});
				// this.js()
				this.onSocketMessage()

			},
			//安卓推送
			NotificationPush() {
				//#ifdef APP-PLUS
				const module = uni.requireNativePlugin("WT-PushModule");
				if (this.testJson.length != 0) {
					for (let i = 0; i < this.testJson.length; i++) {
						console.log(this.testJson[i]);
						const n = module.appPush({
							notificationArray: this.testJson[i]
						});
					}
				}
				// #endif
			},
			//	打开成功监听服务器返回的消息
			onSocketMessage() {
				// 消息
				this.timeout = 30000;
				this.timeoutObj = null;
				uni.onSocketMessage(res => {
					this.getSocketMsg(res.data); // 监听到有新服务器消息
					this.news()
				});
			},
			// 监听到有新服务器消息
			getSocketMsg(reData) {
					// 监听到服务器消息
					// console.log('收到服务器消息', reData);
					const data = JSON.parse(reData)
					try {
						const data1 = JSON.parse(data.msg)
						// console.log(data.msgType)
						if (data.msgType=="sys.notify") {
							this.whether=true
							this.messageId = data1.messageId
							this.content = data1.content;
						}
						else if (data.msgType=="com.plugins.pending.open") {
							this.whether=true
							this.content =data1.title
						}
						else if (data.msgType=="pension.com.alarm.entity.Alarm") {
							this.whether=true
							if(data1.devName=="sos报警"){
								this.content =data1.memberName+data1.alarmDetail
							}else{
								this.content =data1.houseNo+data1.devName+data1.alarmDetail
							}
							if(data1.status=="1"){
								this.content=this.content+"已解除警告"
							}
									
						}else if (data.msgType=="com.rabbitMQ.DirectMessListener.health.notice") {
							this.whether=true
							this.content=data1.name+"膳食提醒"
							var term=data1.term
							if(this.yaoshang[term]==null){
								this.yaoshang[term]=[]
							}
							if(this.yaoshang[term].length==4){
								this.yaoshang[term].shift();
								this.yaoshang[term][3]=data1
							}else{
								// this.yaoshang.[this.i]=data1
								this.yaoshang[term].push(data1)
								// this.i++
							}
							// console.log(data1)
							//缓存药膳菜单
							uni.setStorageSync('yaoshang'+term, this.yaoshang[term]);
						}
						this.msgType=data.msgType
					} catch (e) {
						// console.log('收到错误消息', e);devId 6ceef3be1db638b164bgia
					}
					if(this.whether==true){
						if (data.code == "99") {
							if (data.msg != 'ping') {
								this.testJson.messageId = this.messageId
								this.testJson.content = this.content
								this.testJson.msgType = this.msgType
								this.whether=false
								if(this.testJson.content!=null){
									// console.log(this.testJson.content)
								// #ifdef APP-PLUS
								const module = uni.requireNativePlugin("WT-PushModule");
								const n = module.appPush({
									notificationArray: this.testJson
								});
								// console.log(n)
								// #endif
							}
						}
						}
					}
				},
			
		}
	}
</script>

<style lang="scss">
	@import "styles/index.scss";
	/*每个页面公共css */
	/* 背景颜色 #EBEFF6 */
	page {
		background-color: #EBEFF6;
	}

	/* 苹方字体 */
	@font-face {
		font-family: 'ppf';
		src: url('~@/static/font/pf.ttf');
	}

	* {
		font-family: 'ppf';
	}

	.uni-icons span {
		font-family: UniIcons;
	}

	/* 提交按钮样式 */
	.sub_button {
		height: 38px;
		width: 90.6%;
		background-color: #109EF8;
		color: #FFFFFF;
		line-height: 38px;
		text-align: center;
		margin: 48px auto 0 auto;
		border-radius: unset;
	}
</style>
